home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 May / Macworld (1998-05).dmg / Serious Demos / Lasso 2.5 Test Drive / Lasso 2.5 CGI / Java / LassoProxy / UTF8Coder.java < prev   
Text File  |  1997-12-12  |  2KB  |  88 lines

  1. package blueworld.lasso;
  2.  
  3. import java.io.*;
  4.  
  5. public class UTF8Coder
  6. {
  7.     private UTF8Coder() { }
  8.     
  9.     // This method fixes a bug in DataInputStream.readUTF()
  10.     // where a break is missing from the last case in the switch statement
  11.     public static String decode(String input) throws IOException 
  12.     {
  13.         DataInputStream        inStream = new DataInputStream(new StringBufferInputStream(input));
  14.         int utflen = input.length();
  15.         char str[] = new char[utflen];
  16.         int count = 0;
  17.         int strlen = 0;
  18.         while (count < utflen) 
  19.         {
  20.             int c = inStream.readUnsignedByte();
  21.             int char2, char3;
  22.             switch (c >> 4) 
  23.             { 
  24.                 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  25.                     // 0xxxxxxx
  26.                     count++;
  27.                     str[strlen++] = (char)c;
  28.                     break;
  29.                 case 12: case 13:
  30.                     // 110x xxxx   10xx xxxx
  31.                     count += 2;
  32.                     if (count > utflen) 
  33.                         throw new UTFDataFormatException();          
  34.                     char2 = inStream.readUnsignedByte();
  35.                     if ((char2 & 0xC0) != 0x80)
  36.                         throw new UTFDataFormatException();          
  37.                     str[strlen++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
  38.                     break;
  39.                 case 14:
  40.                     // 1110 xxxx  10xx xxxx  10xx xxxx
  41.                     count += 3;
  42.                     if (count > utflen) 
  43.                         throw new UTFDataFormatException();          
  44.                     char2 = inStream.readUnsignedByte();
  45.                     char3 = inStream.readUnsignedByte();
  46.                     if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
  47.                         throw new UTFDataFormatException();          
  48.                     str[strlen++] = (char)(((c & 0x0F) << 12) |
  49.                                ((char2 & 0x3F) << 6) |
  50.                                ((char3 & 0x3F) << 0));
  51.                     break; // BUG FIX!
  52.                 default:
  53.                     // 10xx xxxx,  1111 xxxx
  54.                     throw new UTFDataFormatException();          
  55.             }
  56.         }
  57.         return new String(str, 0, strlen);
  58.     }
  59.     
  60.     public static String encode(String input) throws IOException
  61.     {
  62.         int strlen = input.length();
  63.         
  64.         ByteArrayOutputStream result = new ByteArrayOutputStream(strlen);
  65.         
  66.         for (int i = 0 ; i < strlen ; i++) 
  67.         {
  68.             int c = input.charAt(i);
  69.             if ((c >= 0x0001) && (c <= 0x007F)) 
  70.             {
  71.                 result.write(c);
  72.             }
  73.             else if (c > 0x07FF) 
  74.             {
  75.                 result.write(0xE0 | ((c >> 12) & 0x0F));
  76.                 result.write(0x80 | ((c >>  6) & 0x3F));
  77.                 result.write(0x80 | ((c >>  0) & 0x3F));
  78.             }
  79.             else
  80.             {
  81.                 result.write(0xC0 | ((c >>  6) & 0x1F));
  82.                 result.write(0x80 | ((c >>  0) & 0x3F));
  83.             }
  84.         }
  85.         return result.toString(0);
  86.     }
  87. }
  88.